#include<string>
#include<algorithm>
using namespace std;
class Solution {
private:
    bool isAdditiveNumber(const string& num,int index,unsigned long long first, unsigned long long second) {
        if (index >= num.size())
            return true;
        unsigned long long tmp = 0;
        bool flag = num[index] == '0';
        while (index < num.size()) {
            tmp = tmp * 10 + num[index] - '0';
            if (tmp == first + second)
                return isAdditiveNumber(num, index + 1, second, first + second);
            if (flag)
                break;
            ++index;
        }
        return false;
    }

public:
    bool isAdditiveNumber(string num) {
        unsigned long long first = 0, second = 0;
        for (int i = 0; i < num.size()-1; ++i) {
            first = first * 10 + num[i] - '0';
            second = 0;
            for (int j = i + 1; (static_cast<int>(num.size())-j-1)>=max(i+1,j-i); ++j) {
                second = second * 10 + num[j] - '0';
                if (isAdditiveNumber(num, j + 1, first, second))
                    return true;
                if (num[i+1] == '0')
                    break;
            }
            if (num[0] == '0')
                break;
        }
        return false;
    }
};